package com.ruoyi.web.controller.smile;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.smile.domain.McDynamicContent;
import com.ruoyi.smile.service.IMcDynamicContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动态内容Controller
 *
 * @author xin
 * @date 2025-03-08
 */
@RestController
@RequestMapping("/smile/content")
public class McDynamicContentController extends BaseController
{
    @Autowired
    private IMcDynamicContentService mcDynamicContentService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;
    /**
     * 查询动态内容列表
     */
//    @PreAuthorize("@ss.hasPermi('smile:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(McDynamicContent mcDynamicContent)
    {
        startPage();
        List<McDynamicContent> list = mcDynamicContentService.selectMcDynamicContentList(mcDynamicContent);
        list.forEach(it->{
            Long userId = it.getUserId();
            if (StringUtils.isNotNull(userId))
            {
                SysUser sysUser = userService.selectUserById(userId);
                sysUser.setPassword("");
                it.setUserInfo(sysUser);
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出动态内容列表
     */
    @PreAuthorize("@ss.hasPermi('smile:content:export')")
    @Log(title = "动态内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, McDynamicContent mcDynamicContent)
    {
        List<McDynamicContent> list = mcDynamicContentService.selectMcDynamicContentList(mcDynamicContent);
        ExcelUtil<McDynamicContent> util = new ExcelUtil<McDynamicContent>(McDynamicContent.class);
        util.exportExcel(response, list, "动态内容数据");
    }

    /**
     * 获取动态内容详细信息
     */
//    @PreAuthorize("@ss.hasPermi('smile:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        McDynamicContent mcDynamicContent = mcDynamicContentService.selectMcDynamicContentById(id);
            Long userId = mcDynamicContent.getUserId();
            if (StringUtils.isNotNull(userId))
            {
                SysUser sysUser = userService.selectUserById(userId);
                sysUser.setPassword("");
                mcDynamicContent.setUserInfo(sysUser);
            }
        return success(mcDynamicContent);
    }

    /**
     * 新增动态内容
     */
    @PreAuthorize("@ss.hasPermi('smile:content:add')")
    @Log(title = "动态内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody McDynamicContent mcDynamicContent)
    {
        System.out.println(mcDynamicContent.toString());
        return toAjax(mcDynamicContentService.insertMcDynamicContent(mcDynamicContent));
    }

    /**
     * 修改动态内容
     */
    @PreAuthorize("@ss.hasPermi('smile:content:edit')")
    @Log(title = "动态内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody McDynamicContent mcDynamicContent)
    {
        return toAjax(mcDynamicContentService.updateMcDynamicContent(mcDynamicContent));
    }

    /**
     * 删除动态内容
     */
    @PreAuthorize("@ss.hasPermi('smile:content:remove')")
    @Log(title = "动态内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mcDynamicContentService.deleteMcDynamicContentByIds(ids));
    }
}
